// pages/order/order.js
const Format = require("../../utils/time.js");
var app = getApp()
var getList = function(that) {
    app.getApi.getProducts("POST", {
        pageNumber: that.data.pageNumber,
        pageSize: 10,
        method: "opg.orderHead.find",
        unionId: app.globalData.unionId
    }).then(data => {
        var list = that.data.items;

        var newTime = new Date().getTime()

        for (let i = 0; i < data.data.result.length; i++) {
            if (newTime > Number(data.data.result[i].eventEndTime)) {
                data.data.result[i].endStatus = true //活动是否结束
            } else {
                data.data.result[i].endStatus = false
            }
            var startTime = data.data.result[i].startTimeStr;
            var ssdat = new Date(startTime.substring(0, 4), parseInt(startTime.substring(5, 7)) - 1, startTime.substring(8, 10))
            var stateMonth = Format.getMonth(parseInt(startTime.substring(5, 7)), that.data.isChinese),
                stateDay = parseInt(startTime.substring(8, 10)) + (that.data.isChinese ? '日 ' : ' '),
                stateWeek = Format.getWeek(ssdat.getDay(), that.data.isChinese),
                stateHours = (ssdat.getHours() > 12 ? 24 - ssdat.getHours() : ssdat.getHours() == 0 ? 12 : ssdat.getHours()) + (startTime.substring(13, 16) + (ssdat.getHours() >= 12 ? "pm" : "am"));
            data.data.result[i].startTimeStr = stateMonth + stateDay + stateWeek + " " + stateHours
            var endTime = data.data.result[i].endTimeStr;
            var endat = new Date(endTime.substring(0, 4), parseInt(endTime.substring(5, 7)) - 1, endTime.substring(8, 10))
            var endMonth = Format.getMonth(parseInt(endTime.substring(5, 7)), that.data.isChinese),
                endDay = parseInt(endTime.substring(8, 10)) + (that.data.isChinese ? '日 ' : ' '),
                endWeek = Format.getWeek(endat.getDay(), that.data.isChinese),
                endHours = (endat.getHours() > 12 ? 24 - endat.getHours() : endat.getHours() == 0 ? 12 : endat.getHours()) + (endTime.substring(13, 16) + (endat.getHours() >= 12 ? "pm" : "am"));
            // data.data.result[i].endTimeStr = endMonth + endDay + endWeek + " " + endHours
            if ((data.data.result[i].eventEndTime - data.data.result[i].submitTime) / 86400000 >= 1) {
                data.data.result[i].endTimeStr = endTime.substring(0, 4) + (that.data.isChinese ? '年 ' : ' ') + endMonth + endDay + endWeek + " " + endHours
            } else {
                data.data.result[i].endTimeStr = endHours
            }
            list.push(data.data.result[i])
        }
        if (Math.ceil(data.data.totalCount / 10) == that.data.pageNumber) {
            that.setData({
                isAll: true
            })
        }
        that.setData({
            items: list,
            loadMore: false,
            onLoadMore: true
        })
        wx.stopPullDownRefresh()
    })
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageNumber: 1,
        isNotEndNow: 1,
        hidden: false, //没有更多
        loadMore: false, //是否显示加载更多
        onLoadMore: false,
        isAll: false,
        items: [],
        isChinese: wx.getStorageSync("isChinese")

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var that = this
        that.setData({
            pageNumber: 1,
            isNotEndNow: 1,
            hidden: false, //没有更多
            loadMore: false, //是否显示加载更多
            onLoadMore: false,
            isAll: false,
            items: [],
            isChinese: wx.getStorageSync("isChinese")
        })
        wx.setNavigationBarTitle({
            title: that.data.isChinese ? "我的订单" : "My Orders"
        })
        getList(that)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReachBottom() {
        var that = this
        if (!that.data.isAll) {
            that.setData({
                pageNumber: that.data.pageNumber + 1,
                loadMore: true
            })
            getList(that)
        } else {
            that.setData({
                hidden: true
            })
        }
    },
    orderDetail(e) {
        var id = e.currentTarget.dataset.id;
        var endStatus = e.currentTarget.dataset.endStatus
        if (!endStatus) {
            wx.navigateTo({
                url: '../orderDetails/orderDetails?id=' + id,
                success: function(res) {},
                fail: function(res) {},
                complete: function(res) {},
            })
        }

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    again(e) {
        let id = e.currentTarget.dataset.id
        wx.navigateTo({
            url: '../activityDetails/activityDetails?id=' + id,
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
        })
    },
    onPullDownRefresh: function() {
        this.onLoad()
    },
})